package com.yzg.simple.netty.hartbeat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author misterWei
 * @mailbox mynameisweiyan@gmail.com
 *
 * Netty 检测心跳机制
 */
public class NettyHeartbeatServer {


    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossLoopGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerLoopGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap()
                           .group(bossLoopGroup,workerLoopGroup)
                              .option(ChannelOption.SO_BACKLOG,128)
                                //在bossLoopGroup 中设置 日志处理器
                                .handler(new LoggingHandler(LogLevel.INFO))
                                  .channel(NioServerSocketChannel.class)
                                   .childOption(ChannelOption.SO_KEEPALIVE,true)
                                    .childHandler(new NettyHeartbeatServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(6666).sync();
            channelFuture.channel().closeFuture().sync();
        }finally {
            bossLoopGroup.shutdownGracefully();
            workerLoopGroup.shutdownGracefully();
        }
    }
}
